import { throttle } from 'lodash-es'
import type { ObjectDirective } from 'vue'
export const v_throttle = (): ObjectDirective => {
  return {
    beforeMount(el, { value }) {
      if (typeof value !== 'function') throw new Error(`${value}必须是一个函数`)
    },
    mounted(el, { value, arg = 500 }) {
      const throttleFn = throttle(value, Number(arg))
      el.addEventListener('click', throttleFn)
      el.debounceFn = throttleFn
    },
    unmounted(el) {
      el.removeEventListener('click', el.throttleFn)
      el.throttleFn = null
    }
  }
}
